﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SharedGenomics.Domain;
using SharedGenomics.Core;
using SharedGenomics.DataTransfer;
using SharedGenomics.Workbench.Views;
using SharedGenomics.Workbench.Models;
using SharedGenomics.Workbench.PresentationModel;

namespace SharedGenomics.Workbench.Views.WindowsForms
{
    public partial class AnalysisResultsFilterView : View, IAnalysisResultsFilterView
    {
        private int _visibleSplitterDistance;
        private bool _isHidden;
        private IAvailableJobsModel _model;

        public AnalysisResultsFilterView()
        {
            InitializeComponent();

            this._isHidden = false;
            this._visibleSplitterDistance = this.spcMain.SplitterDistance;

            this.cmbSubmittedAfter.DataBindings["SelectedValue"].Format += this.ComboboxBinding_Format;
            this.btToggleVisible.Click += (o, e) => { this.ToggleVisibleClicked.Raise(this); };
            this.btFindJobs.Click += (o, e) => { this.FindJobsClicked.Raise(this); };
            this.btLoad.Click += (o, e) => { this.LoadJobClicked.Raise(this); };
            this.jobsBindingSource.CurrentChanged += (o, e) => { this.SelectedJobChanged.Raise(this); };
        }

        private void ComboboxBinding_Format(object sender, ConvertEventArgs e)
        {
            if (e.DesiredType == typeof(string) && e.Value == null)
            {
                e.Value = "Any";
            }
        }

        public void ToggleVisible()
        {
            if (this._isHidden)
            {
                //this.spcMain.SplitterDistance = this._visibleSplitterDistance;
                this.spcMain.Panel1.BackColor = Color.LightSteelBlue;
                this.btToggleVisible.Text = "Hide";
            }
            else
            {
                this.spcMain.Panel1.BackColor = Color.Brown;
                this.btToggleVisible.Text = "Show Jobs";
            }

            this._isHidden = !this._isHidden;
        }

        public int MinimumHeight
        {
            get { return this.spcMain.Panel1.Height; }
        }

        public IAvailableJobsModel Model
        {
            private get { return this._model; }
            set
            {
                this._model = value;
                this.jobsBindingSource.DataSource = value;
            }
        }

        public event EventHandler FindJobsClicked;
        public event EventHandler LoadJobClicked;
        public event EventHandler ToggleVisibleClicked;

        public IEnumerable<ValueDisplayItem<DateTime>> JobSubmissionTimes
        {
            set { this.jobSubmissionTimesBindingSource.DataSource = value; }
        }

        public IEnumerable<ValueDisplayItem<JobType>> JobTypes
        {
            set { this.jobTypesBindingSource.DataSource = value; }
        }

        public IEnumerable<ValueDisplayItem<JobStatus>> JobStatuses
        {
            set { this.jobStatusBindingSource.DataSource = value; }
        }

        public DateTime SelectedSubmissionTime
        {
            get { return (DateTime)this.cmbSubmittedAfter.SelectedValue; }
            set { this.cmbSubmittedAfter.SelectedValue = value; }
        }

        public JobType SelectedJobType
        {
            get { return (JobType)this.cmbJobType.SelectedValue; }
            set { this.cmbJobType.SelectedValue = value; }
        }

        public JobStatus SelectedJobStatus
        {
            get { return (JobStatus)this.cmbJobStatuses.SelectedValue; }
            set { this.cmbJobStatuses.SelectedValue = value; }
        }

        public JobDetails SelectedJob
        {
            get { return this.jobsBindingSource.Current as JobDetails; }
        }

        public bool IsLoadJobEnabled
        {
            set { this.btLoad.Enabled = value; }
        }

        public event EventHandler SelectedJobChanged;

        public bool IsFindJobsEnabled
        {
            set { this.btFindJobs.Enabled = value; }
        }

        public bool IsFindJobsProgressBarVisible
        {
            set { this.pgbSearch.Visible = value; }
        }
    }
}
